using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIEquipListSort : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIEquipListSort),
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UIEquipListSort
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExButton btnConfirm { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnCancel { protected set; get; }
            public UnityEngine.GameObject toggleSortType { protected set; get; }
            public UnityEngine.GameObject toggleOrder { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnConfirm", out var __tbv0);
                this.btnConfirm = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnCancel", out var __tbv1);
                this.btnCancel = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("toggleSortType", out var __tbv2);
                this.toggleSortType = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("toggleOrder", out var __tbv3);
                this.toggleOrder = __tbv3;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        ListScrollAdapter<UIN_CommonToggleOn> sortTypes;
        ListScrollAdapter<UIN_CommonToggleOn> orders;

        EquipSort currSort = EquipSort.Default;

        protected UIB_UIEquipListSort ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIEquipListSort();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.sortTypes = this.InitListScroll<UIN_CommonToggleOn>(this.ui.toggleSortType, this.OnSortTypeChanged);
            this.sortTypes.InitUnitSelected(UnitSelectType.ByExToggle, this.OnSortTypeSelected);
            this.orders = this.InitListScroll<UIN_CommonToggleOn>(this.ui.toggleOrder, this.OnOrderChanged);
            this.orders.InitUnitSelected(UnitSelectType.ByExToggle, this.OnOrderSelected);


            this.ui.btnConfirm.onClick.AddListener(this.OnClickConfirm);
            this.ui.btnCancel.onClick.AddListener(this.OnClickCancel);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        void OnClickConfirm()
        {
            MsgDispatcher.Broadcast(MsgEventType.EquipSort, this.currSort);
            this.Close();
        }

        void OnClickCancel()
        {
            this.Close();
        }

        protected override void OnDispose()
        {
            this.sortTypes = null;
            this.orders = null;
        }

        protected override void OnShow(UIBaseData data)
        {
            if (data is UD_EquipListSort d)
            {
                currSort.Sync(d.sort);
                int indexSortType = (int)currSort.sortType;
                this.sortTypes.RefillCells(((int)UIEquipSortType.MaxLength), indexSortType);
                int indexOrder = currSort.isDesc ? 0 : 1;
                this.orders.RefillCells(2, indexOrder);
            }
        }

        void OnSortTypeChanged(UIN_CommonToggleOn item, int index)
        {
            string lab = APIUIEquip.GetSortName(index);
            item.Show(index, lab);
        }

        void OnSortTypeSelected(int index, UIN_CommonToggleOn item)
        {
            this.currSort.Sync(index, this.currSort.isDesc);
        }

        void OnOrderChanged(UIN_CommonToggleOn item, int index)
        {
            string lab = APIUIEquip.GetOrderName(index);
            item.Show(index, lab);
        }

        void OnOrderSelected(int index, UIN_CommonToggleOn item)
        {
            this.currSort.Sync(this.currSort.sortType, index == 0);
        }
    }
}
